package cn.tedu.web.controller;

import cn.tedu.web.service.IEarningsService;
import cn.tedu.web.mapper.DepartmentMapper;
import cn.tedu.web.mapper.EarningsMapper;
import cn.tedu.web.entity.Department;
import cn.tedu.web.entity.Earnings;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 * earnings
 * 
 * @author issen
 * @since 2021-10-24
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/api/earnings")
public class EarningsController {

    @Autowired
    public IEarningsService earningsService;
    @Autowired
    public EarningsMapper earningsMapper;
    @Autowired
    public DepartmentMapper departmentMapper;

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public ResponseEntity<Object> save(@RequestBody Earnings earnings) {
        earningsService.save(earnings);
        return new ResponseEntity<>("保存成功", HttpStatus.OK);
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Object> delete(@PathVariable("id") Long id) {
        earningsService.removeById(id);
        return new ResponseEntity<>("删除成功", HttpStatus.OK);
    }

    @ApiOperation(value = "条件查询")
    @PostMapping("/get")
    public ResponseEntity<Object> list(@RequestBody Earnings earnings) {
        QueryWrapper<Earnings> qw = new QueryWrapper<>();
        // List<Earnings> earningsList = earningsService.list(qw);
        List<Earnings> earningsList = earningsMapper.listAll();

        return new ResponseEntity<>(earningsList, HttpStatus.OK);
    }

    @ApiOperation(value = "查询所有")
    @GetMapping("/getEarnings")
    public ResponseEntity<Object> getEarnings() {
        QueryWrapper<Earnings> qw = new QueryWrapper<>();
        // List<Earnings> earningsList = earningsService.list(qw);
        List<Earnings> earningsList = earningsMapper.listAll();

        return new ResponseEntity<>(earningsList, HttpStatus.OK);
    }

    @ApiOperation(value = "查询所有")
    @GetMapping("/getDepartment")
    public ResponseEntity<Object> getDepartment() {
        QueryWrapper<Department> qw = new QueryWrapper<>();
        // List<Earnings> earningsList = earningsService.list(qw);
        List<Department> departments = departmentMapper.selectList(qw);

        return new ResponseEntity<>(departments, HttpStatus.OK);
    }

    @ApiOperation(value = "列表（分页）")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Object list(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        IPage<Earnings> page = earningsService.page(new Page<>(pageNum, pageSize), null);
        return new ResponseEntity<>(page, HttpStatus.OK);
    }

    @ApiOperation(value = "详情")
    @GetMapping("/get/{id}")
    public ResponseEntity<Object> get(@PathVariable("id") Long id) {
        Earnings earnings = earningsService.getById(id);
        return new ResponseEntity<>(earnings, HttpStatus.OK);
    }

    @ApiOperation(value = "根据id修改")
    @PutMapping("/update/{id}")
    public ResponseEntity<Object> update(@PathVariable("id") Long id, @RequestBody Earnings earnings) {
        earningsService.updateById(earnings);
        return new ResponseEntity<>("更新成功", HttpStatus.OK);
    }

}
